package zhao.lulu.springbootmybatisxmldev.demos;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.testng.annotations.Test;

import zhao.lulu.springbootmybatisxmldev.mapper.EmpMapper;
import zhao.lulu.springbootmybatisxmldev.pojo.Emp;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;


public class MybatisTest {
    public static InputStream resourceAsStream;

    //获得session工厂对象
    public static SqlSessionFactory sqlSessionFactory;
    //获得session回话对象
    public static SqlSession sqlSession;
    public static EmpMapper mapper;

    static {
        try {
            resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");//获得核心配置文件
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
            sqlSession = sqlSessionFactory.openSession(true);
            mapper = sqlSession.getMapper(EmpMapper.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    //删除操作
    public void test() throws IOException {

        Emp emp = Emp.builder().id(11).username("赵子龙").password("123456")
                .name("李明")
                .gender((short) 2).image("lulu.jpg").job((short) 2).build();
        mapper.addEmp(emp);
        //释放资源
        sqlSession.close();
    }

    @Test
    public void testDel() {
        Emp emp = Emp.builder().id(10).username("Sanbadao").password("123456")
                .name("林风")
                .gender((short) 2).image("lulu.jpg").job((short) 2).build();
        mapper.allEmp().stream().forEach(System.out::println);
        //释放资源
        sqlSession.close();
    }

    @Test
    public void findByIdsTest(){
        List<Integer>ids= Arrays.asList(2,3,6,4);

        mapper.findByIds(ids).stream().forEach(System.out::println);
    }


}


